package com.hundsun.cprs.yyt.service.account.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.service.account.dao.IntegralLogDao;
import com.hundsun.cprs.yyt.service.account.domain.IntegralLog;
import com.hundsun.cprs.yyt.service.account.service.IntegralService;
import com.hundsun.cprs.yyt.service.common.dto.IntegralInfo;
import com.hundsun.cprs.yyt.service.common.service.RemoteShopService;

@Service
public class IntegralServiceImpl extends BaseService implements IntegralService {
    
    @Autowired
    private IntegralLogDao integralLogDao;
    
    @Autowired
    private RemoteShopService remoteShopService; 

    @Override
    @Transactional
    public void addIntegeral(IntegralLog log) {
        integralLogDao.insert(log);
    }

    @Override
    @Transactional
    public void addIntegeral(Long userId, Integer amount, String outBizNo, String memo) {
        //创建积分流水
        IntegralLog integralLog = new IntegralLog();
        integralLog.setAmount(amount);
        integralLog.setUserId(userId);
        integralLog.setStatus(0);
        integralLog.setOutBizNo(outBizNo);
        integralLog.setMemo(memo);
        integralLogDao.insert(integralLog);
        
        //报送积分系统
        sendIntegral(integralLog);
    }
    
    protected void sendIntegral(IntegralLog integralLog){
        //直接对接商城
        IntegralInfo integralInfo = new IntegralInfo();
        integralInfo.setUserId(integralLog.getUserId());
        integralInfo.setAmount(integralLog.getAmount().longValue());
        integralInfo.setMemo(integralLog.getMemo());
        try {
            remoteShopService.changeIntegral(integralInfo);
            integralLog.setStatus(1);
        } catch (Exception e) {
            logger.error("上报积分系统异常:",e);
            integralLog.setStatus(2);
        }
        integralLogDao.updateByPrimaryKey(integralLog);
    }

}
